
local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.Server_PlayerComponent(),script.Parent.Name);
local EventList = ServerOHOH.load.EventList();

local Tool=	_G.CommonOHOH.load.RngTool()
local CarTool=	_G.CommonOHOH.load.CarTool()


function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
    self.player=parent
	self.DataBase=self.parent:GetComponent(ServerOHOH.load.Server_PlayerDataComponent())

    self.RngDataCtrl=self.parent:GetComponent(_G.CommonOHOH.load.Common_RngDataLayer())

    self.lastTime=0

    if self.DataBase:GetData("rollSetting").isAuto then
        self:OpenAuto()
    end

    --
    self.player.UserDataEvent:Connect(function(data)
        if data["rollSetting"] and self.DataBase:GetData("rollSetting").isAuto then
            self:OpenAuto()
        end;
    end);

    --存档Roll出来的部件
    self.player.RollResultEvent:Connect(function(data)

         local carstore=self.DataBase:GetData("car")

         for _,id in pairs(data) do
            if not carstore.had[id] then
                carstore.had[id]=1
            else
                carstore.had[id]=carstore.had[id]+1
            end
         end

         self.DataBase:Execut(function()
         end);
        --  self.player:SynchDataToClient("car");
    end)

end;

--关闭自动
function M.CloseAuto(self)
    if self.AutoCon then
        self.AutoCon:Disconnect()
        self.AutoCon=nil
    end
end

--开启自动
function M.OpenAuto(self)

    if self.AutoCon then
        return 
    end
    self.AutoCon=self:AddUpdate(function()
        self:AddProcess(function()
            self:Roll()
        end)
    end)
end


function M.IsAuto(self)
    if self.AutoCon then
        return true
    else
        return false
    end
end

function M.SetIsAuto(self,isOpen)
    local state=0
    if isOpen then
        state=1
        self:OpenAuto()
    else
        state=0
        self:CloseAuto()
    end
    self.DataBase:GetData("rollSetting").isAuto=state
    self.player:SynchDataToClient("rollSetting");
end


--开始一次随机滚动
function M.Roll(self)

    if  os.clock()-self.lastTime>self.RngDataCtrl:GetRollCd() then
        self.lastTime=os.clock()
        local count=self.RngDataCtrl:GetRollCount()
        local baseLuck=self.RngDataCtrl:GetLucky()
        local ranLuck=self.RngDataCtrl:GetGradeLuckyList()
        local grade=self.RngDataCtrl:GetPredeterminedGrade()
    
        local data={}
        for i = 1, count, 1 do
           local a,b= Tool.Roll(baseLuck,ranLuck,grade)
           local carId=CarTool.GetCarId(a,b)
           table.insert(data,carId)
    
        --    local d1=CarTool.GetPartAllInfo(carId)
        --    ServerOHOH:PrintTable(d1)
        end
        self.player.RollResultEvent:Action(data)
        self:Fire(EventList.one_roll,data)
    end
end

function M.dtor(self)
	M.super.dtor(self);
end;



--开启一次Roll
function M.OneRoll(self)
    if not self:IsAuto()  then
        self:Roll()
    end
end


M:RegistEvent(EventList.roll_one,M.Roll)
M:RegistEvent(EventList.setIsAuto_roll,M.SetIsAuto)


return M
